package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductVo;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.domain.vo.ExportVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

//合同管理页面
@Component
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {
    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

     @Reference
     private ExportProductService exportProductService;

    @RequestMapping("contractList")
    public String contractList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "3") Integer pageSize){
        //1.构建条件对象
        ContractExample contractExample=new ContractExample();
        ContractExample.Criteria criteria = contractExample.createCriteria();
        //根据用户id查询购销合同列表
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //只显示状态为1的列表
        criteria.andStateEqualTo(1);

        //分页查询
        PageInfo<Contract> pageInfo = contractService.findByPage(contractExample, pageNum, pageSize);

        request.setAttribute("pageInfo",pageInfo);

        return "/cargo/export/export-contractList";
    }

    /**
     * 功能入口：
     * 请求参数：pageNum,pageSize
     * 请求地址：list
     * 响应地址：export-contractList.jsp
     * @return
     */
    //报运单列表
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "3") Integer pageSize){
        //1.构建条件对象
        ExportExample exportExample=new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        //根据用户id查询报运单
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        //分页查询
        PageInfo<Export> pageInfo = exportService.findByPage(exportExample, pageNum, pageSize);

        request.setAttribute("pageInfo",pageInfo);

        return "cargo/export/export-list";
    }


    /**
     * 请求地址：/cargo/export/toExport.do
     * 请求参数：id
     * 响应地址：cargo/export/export-toExport
     */

    //进入报运页面
    @RequestMapping("toExport")
    public String toExport(String id){

        /*
        若是前端传过来多个id
        这里用String接收，自动，隔开
        也可以用数组
         */

         request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    //保存数据
    /**
     * 请求数据：/cargo/export/edit.
     * 响应地址：/cargo/export/list
     * 请求参数：Export
     */
    @RequestMapping("edit")
    public String edit(Export export){
        export.setCompanyId(getLoginCompanyId());
        export.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(export.getId())){
            exportService.save(export);
        }else {
            exportService.update(export);
        }


        return "redirect:/cargo/export/list";
    }


    /**
     * 修改-回显
     * 请求地址： /cargo/export/toUpdate.do
     * 请求参数：id
     * 响应地址：export-update.jsp
     */
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        //1.根据报运单的id 查询报运单的数据
        Export export = exportService.findById(id);


        //查询报运单id查询商品列表
       ExportProductExample exportProductExample=new ExportProductExample();
       exportProductExample.createCriteria().andExportIdEqualTo(id);

      List<ExportProduct>eps= exportProductService.findAll(exportProductExample);

      //保存数据传前端
        request.setAttribute("export",export);
        request.setAttribute("eps",eps);



        return "cargo/export/export-update";
    }

/**
 * /**
 * 请求地址：/cargo/export/submit.do
 * 响应地址：/list
 * 请求参数：报运单id
 * //需求：提交把报运单的状态改为1
 */
@RequestMapping("submit")
    public String submit(String id){
    //创建一个报运单对象
    Export export = new Export();
    export.setId(id);
    export.setState(1);
    exportService.update(export);
    return "redirect:/cargo/export/list";
}

/**
 * 请求地址：/cargo/export/cancel.do
 */

@RequestMapping("cancel")
public String cancel(String id) {
    //创建一个报运单对象
    Export export = new Export();
    export.setId(id);
    export.setState(0);
    exportService.update(export);
    return "redirect:/cargo/export/list";
}

/**
 * 电子报运：需求：将从数据库中封装数据到vo对象中，在远程调用海关传过去
 * 请求地址：/cargo/export/exportE.do
 * 响应地址：return "redirect:/cargo/export/list";
 * 请求参数：运单id
 */
@RequestMapping("/exportE")
//封装报运单
    public String exportE(String id){
    //1.封装webservice请求的对象，创建需要封装的对象
    ExportVo exportVo=new ExportVo();
    //1.1 封装报运单信息
    //1.1.1 根据报运单id查询,查询到报运单的对象
    Export export = exportService.findById(id);
    //1.1.2 对象拷贝 org.springframework.beans
    BeanUtils.copyProperties(export,exportVo);
    //1.1.3 设置报运单id
    exportVo.setExportId(id);

    //2.封装商品
    //1.2 封装报运单商品信息
    //1.2.1 获取要封装的商品集合（已经实例化）//获取exportVo对象里面一个属性封装的对象
    List<ExportProductVo> products = exportVo.getProducts();
    //1.2.2 根据报运单id查询商品
    ExportProductExample exportProductExample=new ExportProductExample();
    // 查询条件：export_id 报运单id
    exportProductExample.createCriteria().andExportIdEqualTo(id);
    // select * from co_export_product where export_id=?//查询到商品
    List<ExportProduct> productList = exportProductService.findAll(exportProductExample);

    //1.2.3 遍历商品集合
    if (productList!=null){
        //遍历得到每一个商品对象
        for (ExportProduct exportProduct : productList) {
            //1.2.4 创建webservice请求需要的报运单的商品对象、并封装、添加到集合
            ExportProductVo exportProductVo=new ExportProductVo();

               BeanUtils.copyProperties(exportProductVo,exportProduct);

            // 设置报运单id
            exportProduct.setExportId(id);

            // 设置商品id
            exportProduct.setId(exportProduct.getId());

            // 添加到集合
            products.add(exportProductVo);
        }

    }
    //2.电子报运：海关报运平台录入报运信息:指客户端将封装好的数据传给海关
   WebClient.create("http://locahost:8081/ws/export/user").post(exportVo);


    //3.电运报表，查询报表结果
    ExportResult exportResult=WebClient.create("http://locahost:8081/ws/export/user/1")
            .get(ExportResult.class);
    //4.修改数据库，更改报运单状态，备注，商品交税金额
    exportService.updateExport(exportResult);

    return "redirect:/cargo/export/list";
 }
}
